package com.itheima.controller;

//检查项

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckitemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private CheckitemService checkitemService;

    //添加检查项
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) throws InterruptedException {
        //System.out.println(123);
        //System.out.println(checkItem);
        //Thread.currentThread().sleep(2000);//毫秒
        try {
            checkitemService.add(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);//添加时后台出错返回的结果

        }

        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);//添加时后台成功返回的结果

    }


//    分页查询所有检查项
    @RequestMapping("/queryAll")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result queryAll(@RequestBody QueryPageBean queryPageBean){
        try {
        Integer currentPage = queryPageBean.getCurrentPage(); //当前第几页
        Integer pageSize = queryPageBean.getPageSize();//每页显示多少条数据
        String queryString = queryPageBean.getQueryString();//查询的关键字

        PageResult _queryAll = checkitemService.queryAll(currentPage, pageSize, queryString);

        Long total = _queryAll.getTotal(); //总记录数
        int _currentPage =(int) Math.ceil((total*1.0) / (pageSize*1.0));//总页数
//            System.out.println("总记录数"+total);
//            System.out.println("数据库查出真实的总页数"+_currentPage);
//            System.out.println("要查的当前页数"+currentPage);
        /*
         * _currentPage  当前数据库查出的真实的总页数
         * currentPage   前端传过来的要查的当前页码
         * 如果要查的当前页码大于真实的总页数,那么就需要把当前页码的值赋值给总页数进行重新查询并返回
         * */
        if(currentPage>_currentPage){
            PageResult queryAll = checkitemService.queryAll(_currentPage, pageSize, queryString);
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,queryAll);
        }
        return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,_queryAll);

    } catch (Exception e) {
        e.printStackTrace();
        return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
    }
}


//        Result result = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, queryAll);
//       // System.out.println(result);
//        return result;




//    根据ID删除检查项
@PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/DeleteById")
    public Result DeleteById(Integer id){
        try {
            boolean delete = checkitemService.DeleteById(id);
            if(delete){
                //删除成功 :
                return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
            }else {
                //删除失败
                return new Result(false,"该检查项已被其他检查组引用");
            }
        } catch (Exception e) {
            //后台系统出错
            e.printStackTrace();
            return new Result(false,"系统出错了....");

        }
    }

//    根据ID查询检查项
    @RequestMapping("/qurey")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result qurey(Integer id){

        try {
            CheckItem qurey = checkitemService.qurey(id);  //查询成功
            return new Result(true,"查询成功",qurey);
        } catch (Exception e) {  //查询失败
            e.printStackTrace();
            return new Result(false,"查询失败，请刷新页面");

        }


    }

    //修改检查项
@RequestMapping("/UpdateById")
@PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result UpdateById(@RequestBody CheckItem checkItem){

    try {
        checkitemService.Update(checkItem);
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    } catch (Exception e) {
        e.printStackTrace();
        return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
    }

}

}
